import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import { RightContent } from '@/components/GlobalHeader';
import { getCookie } from '@/utils/cookie';
// import SentryReport from '@/utils/sentry';
import { getMenuListRight } from '@/services/common';

import '@/styles/index.<%= css %>';

// 设置dayjs中文语言包
dayjs.locale('zh-cn');

// const logo = require('@/assets/logo.png');
let menuRightMap = new Map();

// // 实例化sentry
// const sentry = SentryReport.getInstance({
//   dsn: SENTRY_DSN,
//   release: RELEASE_VERSION,
//   environment: APP_ENV,
// });
// // 挂载到 window 对象上
// window.$sentry = sentry;

// 文档 https://beta-pro.ant.design/docs/initial-state-cn
export async function getInitialState() {
  return {
    token: getCookie('token'),
  };
}

export const layout = ({ initialState }: { [key: string]: any }) => {
  return {
    // 文档 https://beta-pro.ant.design/docs/advanced-menu-cn
    menu: {
      locale: false,
      params: {
        token: initialState.token,
      },
      request: async (params: {}, defaultMenuData: []) => {
        try {
          const menus = await getMenuListRight();
          if (!(menus ?? []).length) {
            return [];
          }
          (menus ?? []).forEach((menu) => {
            menuRightMap.set(`${menu.code}_${menu.serviceId}`, true);
          });
          function formatUseableRoutes(menus: []) {
            menus.forEach((menu: { [key: string]: any }) => {
              const localCode = menu.code;
              if (menuRightMap.has(localCode)) {
                delete menu.hideInMenu;
              } else {
                menu.hideInMenu = true;
              }
              formatUseableRoutes(menu.children ?? []);
            });
          }
          formatUseableRoutes(defaultMenuData);
          return defaultMenuData;
        } catch (e) {
          return [];
        }
      },
    },
    rightRender: () => <RightContent />,
    onError: (error: Error) => {
      console.error(error);
    },
  };
};
